package com.net.tkc.pro.controller;

import com.net.tkc.pro.entity.*;
import com.net.tkc.pro.impl.CollectImpl;
import com.net.tkc.pro.impl.ReportImpl;
import com.net.tkc.pro.tool.JsonUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin //解决跨域问题
@RestController
@RequestMapping("/Report/")
public class ReportController {
    ResultEntity resultEntity = null;
    @Resource
    ReportImpl reportImpl;

    /**
     * 审核原因
     * @param state
     * @param id
     * @return
     */
    @PutMapping("Report")
    private String upPicture(
            @RequestParam("state") int state,
            @RequestParam("id") int id
    ){
        resultEntity = new ResultEntity();
        try{
            int result = reportImpl.upReport(state,id);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("修改成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("修改失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     * 举报人查看举报信息
     * @param userto 举报人
     * @return
     */
    @GetMapping("Report2")
    private String getReportList2(
            @RequestParam("userto") int userto
    ){
        resultEntity = new ResultEntity();
        try{
            List<Report> list =reportImpl.getReportList2(userto);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }

    /**
     *  被举报人查看自己被举报原因
     * @param userfrom 被举报人
     * @return
     */
    @GetMapping("Report")
    private String getReportList(
            @RequestParam("userfrom") int userfrom
    ){
        resultEntity = new ResultEntity();
        try{
            List<Report> list =reportImpl.getReportList(userfrom);
            if (list.size()!=0){
                resultEntity.setCount(list.size());
                resultEntity.setCode("0");
                resultEntity.setData(list);
                resultEntity.setMsg("获取成功！");
            }else{
                resultEntity.setMsg("获取失败,请查看是否有数据");
                resultEntity.setCode("2");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setMsg("获取失败");
            resultEntity.setCode("1");
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
    /**
     * 添加举报
     * @param userfrom 被举报人
     * @param userto 举报人
     * @param reason 举报原因
     * @return
     */
    @PostMapping("Report")
    private String upReport(
            @RequestParam("userfrom") int userfrom,
            @RequestParam("userto") int userto,
            @RequestParam("reason") String reason
    ){
        resultEntity = new ResultEntity();
        DateFormat dateFormat=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date=new Date();
        String Time=dateFormat.format(date);
        Report report=new Report();
        report.setUserfrom(userfrom);
        report.setDttime(Time);
        report.setUserto(userto);
        report.setReason(reason);
        try{
            int result = reportImpl.addReport(report);
            if(result>0){
                resultEntity.setCode("0");
                resultEntity.setMsg("添加成功！");
            }else{
                resultEntity.setCode("1");
                resultEntity.setMsg("添加失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
            resultEntity.setCode("9999");
            resultEntity.setMsg(e.getMessage());
        }
        return JsonUtil.getJsonTool().toJson(resultEntity);
    }
}
